package writer;

import java.io.IOException;
import java.util.ArrayList;
import jxl.CellView;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Modification of Write Excel.java from 
 * www.vogella.de/articles/JavaExcel/article.html
 * 
 * Reads an ArrayList of TestResult objects into Excel sheet
 * 
 * @author jacobbj
 *
 */
public class WriteExcel {

	private WritableCellFormat arialBoldUnderline;
	private WritableCellFormat arial;
	private int sheets = 0; // index for excelsheets
	

	public void addSheet(WritableWorkbook workbook,String sheetName, ArrayList<TestResult> list, String head1, String head2) throws IOException, WriteException {
		// File file = new File(inputFile);
		
		// WorkbookSettings wbSettings = new WorkbookSettings();
		// wbSettings.setLocale(new Locale("en", "EN"));

		// WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
		
		workbook.createSheet(sheetName, sheets);
		//workbook.createSheet("MultiBenchmark", 1);
		
		WritableSheet excelSheet = workbook.getSheet(sheets);
		createLabel(excelSheet, head1 , head2);
		createContent(excelSheet, list);
		
		sheets += 1;
		
		// WritableSheet excelSheet2 = workbook.getSheet(1);

		// workbook.write();
		// workbook.close();
	}
	
	

	private void createLabel(WritableSheet sheet, String c1, String c2) throws WriteException {
		
		// Create an arial font
		WritableFont arial11pt = new WritableFont(WritableFont.ARIAL, 11);
		
		// Define the cell format
		arial = new WritableCellFormat(arial11pt);
		arial.setAlignment(Alignment.CENTRE);
		
		// Automatically wrap the cells
		arial.setWrap(true);

		// Create create a bold font with underlines
		WritableFont arial11ptBoldUnderline = new WritableFont(
				WritableFont.ARIAL, 11, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		
		arialBoldUnderline = new WritableCellFormat(arial11ptBoldUnderline);
		// Lets automatically wrap the cells
		
		arialBoldUnderline.setWrap(true);
		arialBoldUnderline.setAlignment(Alignment.CENTRE);

		CellView cv = new CellView();
		cv.setFormat(arial);
		cv.setFormat(arialBoldUnderline);
		// cv.setAutosize(true);
		cv.setSize(2000);
		
		// Write a few headers
		addCaption(sheet, 0, 0, c1);
		addCaption(sheet, 1, 0, c2);
		
	}

	private void createContent(WritableSheet sheet, ArrayList<TestResult> l) throws WriteException,
			RowsExceededException {

		// current row
		int count = 1;
		
		// Extract all TestResults
		for (TestResult tr : l){
			
			// add result to sheet
						// First column
						addNumber(sheet, 0, count, tr.getNumberOfRuns());
						// Second column
						addLong(sheet, 1, count, tr.getTimeMeasured());
			count++;
		}
		
	}

	private void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, arialBoldUnderline);
		sheet.addCell(label);
	}

	private void addNumber(WritableSheet sheet, int column, int row,
			Integer integer) throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, integer, arial);
		sheet.addCell(number);
	}

	private void addLong(WritableSheet sheet, int column, int row,
			Long l) throws WriteException, RowsExceededException {
		Number number;
		number = new Number(column, row, l, arial);
		sheet.addCell(number);
	}

}